﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SharpSoft.WPF.Controls
{
    public class GeoControl : System.Windows.Controls.Primitives.Thumb
    {
        static GeoControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(GeoControl), new FrameworkPropertyMetadata(typeof(GeoControl)));
        }

        public GeoControl()
        {
            this.ClipToBounds = false;
            this.Designer = new AntiparallelOgramGeoDesigner();
            this.Width = this.Height = 100d;
            this.DragDelta += GeoControl_DragDelta;
        }

        private void GeoControl_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            if (this.Designer != null && this.IsActivated)
            {
                e.Handled = true;
                //var v = new Vector(e.HorizontalChange, e.VerticalChange);
                //this.GetWindow().Title = v.ToString();
                Canvas.SetLeft(this, Canvas.GetLeft(this) + e.HorizontalChange);
                Canvas.SetTop(this, Canvas.GetTop(this) + e.VerticalChange);
            }

        }

        /// <summary>
        /// 关联的几何图形设计器
        /// </summary>
        public IGeoDesigner Designer
        {
            get { return (IGeoDesigner)GetValue(DesignerProperty); }
            set { SetValue(DesignerProperty, value); }
        }

        public static readonly DependencyProperty DesignerProperty =
            DependencyProperty.Register("Designer", typeof(IGeoDesigner), typeof(GeoControl), new PropertyMetadata(default(IGeoDesigner), new PropertyChangedCallback(DesignerChangedCallback)));

        public bool IsActivated
        {
            get { return (bool)GetValue(IsActivatedProperty); }
            set { SetValue(IsActivatedProperty, value); }
        }

        public static readonly DependencyProperty IsActivatedProperty =
            DependencyProperty.Register("IsActivated", typeof(bool), typeof(GeoControl), new PropertyMetadata(default(bool), new PropertyChangedCallback(IsActivatedChanged)));

        private static void IsActivatedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is GeoControl gc && gc.IsActivated)
            {
                gc.BringToFront();
            }
        }

        private static void DesignerChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is GeoControl gc)
            {
                if (e.OldValue is IGeoDesigner gdo)
                {
                    gdo.Updated -= gc.Designer_Updated;
                }
                if (e.NewValue is IGeoDesigner gdn)
                {
                    gdn.Updated += gc.Designer_Updated;
                }
            }
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == BackgroundProperty || e.Property == ForegroundProperty || e.Property == BorderBrushProperty || e.Property == BorderThicknessProperty)
            {
                this.InvalidateVisual();
            }
            else if (e.Property == IsActivatedProperty)
            {
                if (this.Designer != null)
                {
                    this.Designer.IsActivated = this.IsActivated;
                }
                this.InvalidateVisual();
            }
        }

        private void Designer_Updated(object sender, EventArgs e)
        {
            this.InvalidateVisual();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            if (Designer != null)
            {
                Designer.Paint(drawingContext, this);
            }

            // base.OnRender(drawingContext);
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);
            this.IsActivated = true;
        }
        /// <summary>
        /// 将当前控件放到最前面
        /// </summary>
        private void BringToFront()
        {
            if (this.Parent is Panel panel)
            {
                foreach (var item in panel.Children)
                {
                    if (item is UIElement ui)
                    {
                        Panel.SetZIndex(ui, 0);
                        if (ui is GeoControl gc && gc != this)
                        {
                            gc.IsActivated = false;
                        }
                    }
                }
                Panel.SetZIndex(this, 1);
                if (!this.IsActivated)
                {
                    this.IsActivated = true;
                }

            }
        }

    }
}
